<!--  -->
<template>
  <div class="app" ref="list">
    <top-tit :tit="'我的消息'"></top-tit>
    <div class="list">
      <div class="list_item" v-for="item in infoList" :key="item.id">
        <div class="container">
          <div class="info" v-text="item.data.message"></div>
          <div class="time">{{ item.created_at | timeago }}</div>
        </div>
      </div>
    </div>

    <div class="on_down" v-show="!lazyFlag">
        <div class="container">
            已经到底啦
        </div>
    </div>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import topTit from "@/components/topTit";
import Vue from "vue";
export default {
  name: "myMessage",
  //import引入的组件需要注入到对象中才能使用
  components: {
    topTit,
  },
  data() {
    //这里存放数据
    return {
      infoList: [],
      page: 1,
      page_size: 10,
      lazyFlag: true,
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    handleScroll() {
      // console.log(window.scrollY);
      // console.log(document.documentElement.scrollTop);
      // console.log(document.body.scrollTop);
      // console.log(y)
      let _scrollHeight = this.$refs.list.scrollHeight;
      let _scrollTop = this.$refs.list.scrollTop;
      let _windowHeight = window.innerHeight;
      // console.log(_scrollHeight);
      // console.log(_scrollTop);
      // console.log(_windowHeight);
      if (_scrollHeight - _windowHeight == _scrollTop  && this.lazyFlag) {
        this.page = this.page + 1;
        this.$axios
          .get("http://1.14.239.98/api/v2/member/messages", {
            params: {
              page: this.page,
              page_size: this.page_size,
            },
          })
          .then((res) => {
            // console.log(res);
            this.infoList.push(...res.data.data);
            if (this.infoList.length == res.data.total) {
              this.lazyFlag = false
            }
            // console.log(this.infoList);
          });
      }
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  async created() {
    await this.$axios
      .get("http://1.14.239.98/api/v2/member/messages", {
        params: {
          page: this.page,
          page_size: this.page_size,
        },
      })
      .then((res) => {
        // console.log(res);
        this.infoList = res.data.data;
        // console.log(this.infoList);
      });
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    this.$refs.list.addEventListener("scroll", this.handleScroll);
    // console.log("dazai");
  },
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style scoped lang="less">
.app {
  height: 100%;
  overflow-y: scroll;
}
div.list {
  border-top: 1px solid #f1f2f6;
  width: 100%;

  div.list_item {
    width: 100%;

    border-bottom: 1px solid rgba(0, 0, 0, 0.05);
    height: 1.34rem;
    display: flex;
    align-items: center;
    .container {
      width: 6.9rem;
      height: 0.92rem;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: space-between;
      div.info {
        text-align: start;
        font-size: 0.28rem;
        color: #666;
        width: 100%;
      }
      div.time {
        text-align: start;
        font-size: 12px;
        color: #999;
        width: 100%;
      }
    }
  }
}

div.on_down {
    height: 1.58rem;
    background-color: #f6f6f6;
    display: flex;
    align-items: center;
  div.container {
    height: .38rem;
    font-size: .28rem;
    color: #ccc;
  }
}
</style>
